package main

import (
	"fmt"
	"errors"
	"net"
	"net/rpc"
	"os"
	"time"
)

//usage: go run rpc_tcp_server.go
//used with rpc_tcp_client.go

type Args struct {
	A,B int
}

type Quotient struct {
	Quo,Rem int
}

type Arith int

func (t *Arith)Multiply(args *Args,reply *int)error{
	*reply=args.A*args.B
	fmt.Println("call Arith.Multiply ... ",time.Now())
	return nil
}

func (t *Arith)Divide(args *Args,quo *Quotient)error{
	if args.B==0 {
		return errors.New("divide by zero")
	}
	quo.Quo=args.A/args.B
	quo.Rem=args.A%args.B
	fmt.Println("call Arith.Divide ... ",time.Now())
	return nil
}

func main(){
	arith:=new(Arith)
	rpc.Register(arith)
	
	tcpAddr,err:=net.ResolveTCPAddr("tcp4",":1234")
	checkError(err)
	
	listener,err:=net.ListenTCP("tcp",tcpAddr)
	checkError(err)
	
	for {
		conn,err:=listener.Accept()
		if err!=nil {
			continue
		}
		rpc.ServeConn(conn)
	}
}

func checkError(err error) {
    if err != nil {
        fmt.Println("Fatal error ", err.Error())
        os.Exit(1)
    }
}